﻿USE [HelperDatabase]
GO
/****** Object:  User [helper_db_admin]    Script Date: 04/17/2010 23:28:38 ******/
CREATE USER [helper_db_admin] FOR LOGIN [helper_db_admin] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  Table [dbo].[DinhDangMaPhieu]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DinhDangMaPhieu](
	[ID] [int] NOT NULL,
	[LoaiPhieu] [nvarchar](max) NOT NULL,
	[DinhDang] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_DinhDangMaPhieu] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[TrangThaiPhieu]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TrangThaiPhieu](
	[TrangThaiPhieu_ID] [int] NOT NULL,
	[MoTa] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_TrangThaiPhieu] PRIMARY KEY CLUSTERED 
(
	[TrangThaiPhieu_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[QuyenHan]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[QuyenHan](
	[QuyenHan_ID] [int] NOT NULL,
	[TenQuyen] [nvarchar](max) NOT NULL,
	[GhiChu] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_Permission_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_Permission] PRIMARY KEY CLUSTERED 
(
	[QuyenHan_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[NhomHangHoa]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NhomHangHoa](
	[NhomHangHoa_ID] [int] IDENTITY(1,1) NOT NULL,
	[TenNhomHangHoa] [nvarchar](max) NOT NULL,
	[GhiChu] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_ItemGroup_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_ItemGroup] PRIMARY KEY CLUSTERED 
(
	[NhomHangHoa_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[NhomNguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NhomNguoiDung](
	[NhomNguoiDung_ID] [int] IDENTITY(1,1) NOT NULL,
	[TenNhom] [nvarchar](max) NOT NULL,
	[GhiChu] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_UserGroup_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_UserGroup] PRIMARY KEY CLUSTERED 
(
	[NhomNguoiDung_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[NhaCungCap]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NhaCungCap](
	[MaNhaCungCap] [nvarchar](50) NOT NULL,
	[TenNhaCungCap] [nvarchar](max) NOT NULL,
	[TenGiaoDich] [nvarchar](max) NULL,
	[MaThue] [nvarchar](max) NULL,
	[TaiKhoanNganHang] [nvarchar](max) NULL,
	[DiaChi] [nvarchar](max) NULL,
	[DienThoai] [nvarchar](max) NULL,
	[SoFax] [nvarchar](max) NULL,
	[NguoiDaiDien] [nvarchar](max) NULL,
	[Email] [nvarchar](max) NULL,
	[GhiChu] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_Supplier_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_Supplier] PRIMARY KEY CLUSTERED 
(
	[MaNhaCungCap] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhongBan]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhongBan](
	[MaPhongBan] [nvarchar](50) NOT NULL,
	[TenPhongBan] [nvarchar](max) NOT NULL,
	[MoTa] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhongBan_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_PhongBan] PRIMARY KEY CLUSTERED 
(
	[MaPhongBan] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[TrangThaiQuyTrinhMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TrangThaiQuyTrinhMuaHang](
	[ID] [int] NOT NULL,
	[MoTa] [nvarchar](max) NULL,
 CONSTRAINT [PK_TrangThaiQuyTrinhMuaHang] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuDanhGiaNCC]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuDanhGiaNCC](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[Content] [nvarchar](max) NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL CONSTRAINT [DF_SupplierInspectionReceipt_CreateDate]  DEFAULT (getdate()),
	[MaNguoiDuyet] [nvarchar](50) NULL,
	[NgayDuyet] [datetime] NULL,
	[TrangThai] [int] NOT NULL CONSTRAINT [DF_PhieuDanhGiaNCC_XinDuyet]  DEFAULT ((2)),
	[LyDoKhongDuyet] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_SupplierInspectionReceipt_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_SupplierInspectionReceipt] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[NhomNguoiDung_QuyenHan]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NhomNguoiDung_QuyenHan](
	[NhomNguoiDung_ID] [int] NOT NULL,
	[QuyenHanID] [int] NOT NULL,
	[TrangThai] [nvarchar](max) NULL,
 CONSTRAINT [PK_NhomNguoiDung_QuyenHan] PRIMARY KEY CLUSTERED 
(
	[NhomNguoiDung_ID] ASC,
	[QuyenHanID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[HangHoa]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[HangHoa](
	[MaHangHoa] [nvarchar](50) NOT NULL,
	[NhomHangHoa_ID] [int] NULL,
	[TenHangHoa] [nvarchar](max) NOT NULL,
	[DonVi] [nvarchar](max) NULL,
	[Item_Measure] [nvarchar](max) NULL,
	[MucDich] [nvarchar](max) NULL,
	[MoTa] [nvarchar](max) NULL,
	[NhapKho] [real] NOT NULL CONSTRAINT [DF_Item_Stock_in]  DEFAULT ((0)),
	[XuatKho] [real] NOT NULL CONSTRAINT [DF_Item_Stock_out]  DEFAULT ((0)),
	[NhapCuoi] [real] NOT NULL CONSTRAINT [DF_Item_Last_stock_in]  DEFAULT ((0)),
	[XuatCuoi] [real] NOT NULL CONSTRAINT [DF_Item_Last_stock_out]  DEFAULT ((0)),
	[TonKho] [real] NOT NULL CONSTRAINT [DF_Item_Current_stock]  DEFAULT ((0)),
	[TonKhoToiThieu] [real] NOT NULL CONSTRAINT [DF_Item_Min_stock]  DEFAULT ((0)),
	[TonKhoToiDa] [real] NOT NULL CONSTRAINT [DF_Item_Max_stock]  DEFAULT ((0)),
	[GhiChu] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[Gia] [real] NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_Item_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_Item] PRIMARY KEY CLUSTERED 
(
	[MaHangHoa] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[NguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NguoiDung](
	[MaNguoiDung] [nvarchar](50) NOT NULL,
	[Password] [nvarchar](max) NOT NULL,
	[NhomNguoiDung_ID] [int] NULL,
	[MaPhongBan] [nvarchar](50) NULL,
	[ViTriPhongBan] [nvarchar](max) NULL,
	[Ten] [nvarchar](max) NOT NULL,
	[NgaySinh] [datetime] NULL,
	[Email] [nvarchar](max) NULL,
	[ThongTinNhanVien] [nvarchar](max) NULL,
	[TrangThai] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_User_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
	[MaNguoiDung] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[HangHoa_NhaCungCap]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[HangHoa_NhaCungCap](
	[HangHoa_NhaCungCap_ID] [int] IDENTITY(1,1) NOT NULL,
	[MaHangHoa] [nvarchar](50) NOT NULL,
	[MaNhaCungCap] [nvarchar](50) NOT NULL,
	[PhieuDanhGiaNCC_ID] [nvarchar](50) NOT NULL,
	[ChatLuong] [int] NULL,
	[Gia] [real] NULL,
	[HauMai] [int] NULL,
	[ThoiGianGiaoHang] [int] NULL,
	[HinhThucThanhToan] [int] NULL,
	[GhiChu] [nvarchar](max) NULL,
	[Dat] [bit] NOT NULL CONSTRAINT [DF_Item_Supplier_Qualified]  DEFAULT ((0)),
 CONSTRAINT [PK_Item_Supplier] PRIMARY KEY CLUSTERED 
(
	[HangHoa_NhaCungCap_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuDeNghiMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuDeNghiMuaHang](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL,
	[NgayHan] [datetime] NOT NULL,
	[MaNguoiDuyet] [nvarchar](50) NULL,
	[NgayDuyet] [datetime] NULL,
	[NoiDung] [nvarchar](max) NULL,
	[LyDoKhongDuyet] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhieuDeNghiMuaHang_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_PhieuDeNghiMuaHang1] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuMuaHang](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL CONSTRAINT [DF_PhieuMuaHang_NgayTao]  DEFAULT (getdate()),
	[MaNguoiDuyet] [nvarchar](50) NULL,
	[NgayDuyet] [datetime] NULL,
	[NoiDung] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhieuMuaHang_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_PhieuMuaHang] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuTamUng]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuTamUng](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL CONSTRAINT [DF_PhieuTamUng_NgayTao]  DEFAULT (getdate()),
	[MaNguoiDuyet] [nvarchar](50) NULL,
	[NgayDuyet] [datetime] NULL,
	[LyDoKhongDuyet] [nvarchar](max) NULL,
	[SoTien] [real] NOT NULL,
	[ThoiGian] [datetime] NULL,
	[NoiDung] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhieuTamUng_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_PhieuTamUng] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuThanhToan]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuThanhToan](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL CONSTRAINT [DF_PhieuThanhToan_NgayTao]  DEFAULT (getdate()),
	[MaNguoiDuyet] [nvarchar](50) NULL,
	[LyDoKhongDuyet] [nvarchar](max) NULL,
	[NgayDuyet] [datetime] NULL,
	[NoiDung] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhieuThanhToan_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_PhieuThanhToan] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuNhapKho]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuNhapKho](
	[MaPhieu] [nvarchar](50) NOT NULL,
	[MaNguoiTao] [nvarchar](50) NOT NULL,
	[NgayTao] [datetime] NOT NULL,
	[NoiDung] [nvarchar](max) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_PhieuNhapKho_deleted]  DEFAULT ((0)),
	[MaDonDatHang] [nvarchar](100) NULL,
 CONSTRAINT [PK_PhieuNhapKho] PRIMARY KEY CLUSTERED 
(
	[MaPhieu] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PhieuDeNghiMuaHang_HangHoa]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[MaHangHoa] [nvarchar](50) NOT NULL,
	[MaPhieuDeNghiMuaHang] [nvarchar](50) NOT NULL,
	[SoLuongDeNghi] [real] NOT NULL,
	[GiaThamKhao] [real] NULL,
	[NhaCungCapThamKhao] [nvarchar](max) NULL,
	[HangHoa_NhaCungCap_ID] [int] NULL,
	[SoLuongMua] [real] NULL,
	[SoLuongNhapKho] [real] NULL,
 CONSTRAINT [PK_PhieuDeNghiMuaHang_HangHoa] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[QuyTrinhMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[QuyTrinhMuaHang](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TenQuyTrinh] [nvarchar](max) NOT NULL,
	[TrangThai] [int] NOT NULL CONSTRAINT [DF_QuyTrinhMuaHang_TrangThai]  DEFAULT ((0)),
	[MaPhieuDeNghiMuaHang] [nvarchar](50) NULL,
	[MaPhieuMuaHang] [nvarchar](50) NULL,
	[MaPhieuTamUng] [nvarchar](50) NULL,
	[MaPhieuThanhToan] [nvarchar](50) NULL,
	[deleted] [bit] NOT NULL CONSTRAINT [DF_QuyTrinhMuaHang_deleted]  DEFAULT ((0)),
 CONSTRAINT [PK_QuyTrinhMuaHang] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[HangHoaDonDatHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[HangHoaDonDatHang]
as
select qt.ID QuyTrinhID, 
       (qt.MaPhieuMuaHang + hhncc.MaNhaCungCap) MaDonDatHang,
       hn.ID

from   PhieuDeNghiMuaHang_HangHoa hn,
       HangHoa_NhaCungCap hhncc,
       QuyTrinhMuaHang qt,
       PhieuDeNghiMuaHang pdn,
       PhieuMuaHang pmh

where qt.MaPhieuDeNghiMuaHang = pdn.MaPhieu AND
      qt.MaPhieuMuaHang = pmh.MaPhieu AND
      hn.MaPhieuDeNghiMuaHang = pdn.MaPhieu AND
      hn.HangHoa_NhaCungCap_ID = hhncc.HangHoa_NhaCungCap_ID;
GO
/****** Object:  View [dbo].[DonDatHang]    Script Date: 04/17/2010 23:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[DonDatHang]
as
select (qt.MaPhieuMuaHang + hhncc.MaNhaCungCap) MaDonDatHang,
       qt.ID QuyTrinhID, hhncc.MaNhaCungCap

from   PhieuDeNghiMuaHang_HangHoa hn,
       HangHoa_NhaCungCap hhncc,
       QuyTrinhMuaHang qt,
       PhieuDeNghiMuaHang pdn,
       PhieuMuaHang pmh

where qt.MaPhieuDeNghiMuaHang = pdn.MaPhieu AND
      qt.MaPhieuMuaHang = pmh.MaPhieu AND
      hn.MaPhieuDeNghiMuaHang = pdn.MaPhieu AND
      hn.HangHoa_NhaCungCap_ID = hhncc.HangHoa_NhaCungCap_ID

group by qt.MaPhieuMuaHang, hhncc.MaNhaCungCap,
         qt.ID;
GO
/****** Object:  ForeignKey [FK_HangHoa_NhomHangHoa]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[HangHoa]  WITH CHECK ADD  CONSTRAINT [FK_HangHoa_NhomHangHoa] FOREIGN KEY([NhomHangHoa_ID])
REFERENCES [dbo].[NhomHangHoa] ([NhomHangHoa_ID])
GO
ALTER TABLE [dbo].[HangHoa] CHECK CONSTRAINT [FK_HangHoa_NhomHangHoa]
GO
/****** Object:  ForeignKey [FK_HangHoa_NhaCungCap_HangHoa]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[HangHoa_NhaCungCap]  WITH CHECK ADD  CONSTRAINT [FK_HangHoa_NhaCungCap_HangHoa] FOREIGN KEY([MaHangHoa])
REFERENCES [dbo].[HangHoa] ([MaHangHoa])
GO
ALTER TABLE [dbo].[HangHoa_NhaCungCap] CHECK CONSTRAINT [FK_HangHoa_NhaCungCap_HangHoa]
GO
/****** Object:  ForeignKey [FK_Item_Supplier_Supplier]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[HangHoa_NhaCungCap]  WITH CHECK ADD  CONSTRAINT [FK_Item_Supplier_Supplier] FOREIGN KEY([MaNhaCungCap])
REFERENCES [dbo].[NhaCungCap] ([MaNhaCungCap])
GO
ALTER TABLE [dbo].[HangHoa_NhaCungCap] CHECK CONSTRAINT [FK_Item_Supplier_Supplier]
GO
/****** Object:  ForeignKey [FK_Item_Supplier_SupplierInspectionReceipt]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[HangHoa_NhaCungCap]  WITH CHECK ADD  CONSTRAINT [FK_Item_Supplier_SupplierInspectionReceipt] FOREIGN KEY([PhieuDanhGiaNCC_ID])
REFERENCES [dbo].[PhieuDanhGiaNCC] ([MaPhieu])
GO
ALTER TABLE [dbo].[HangHoa_NhaCungCap] CHECK CONSTRAINT [FK_Item_Supplier_SupplierInspectionReceipt]
GO
/****** Object:  ForeignKey [FK_NguoiDung_PhongBan]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[NguoiDung]  WITH CHECK ADD  CONSTRAINT [FK_NguoiDung_PhongBan] FOREIGN KEY([MaPhongBan])
REFERENCES [dbo].[PhongBan] ([MaPhongBan])
GO
ALTER TABLE [dbo].[NguoiDung] CHECK CONSTRAINT [FK_NguoiDung_PhongBan]
GO
/****** Object:  ForeignKey [FK_User_UserGroup]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[NguoiDung]  WITH CHECK ADD  CONSTRAINT [FK_User_UserGroup] FOREIGN KEY([NhomNguoiDung_ID])
REFERENCES [dbo].[NhomNguoiDung] ([NhomNguoiDung_ID])
GO
ALTER TABLE [dbo].[NguoiDung] CHECK CONSTRAINT [FK_User_UserGroup]
GO
/****** Object:  ForeignKey [FK_UserGroup_Permission_Permission]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[NhomNguoiDung_QuyenHan]  WITH CHECK ADD  CONSTRAINT [FK_UserGroup_Permission_Permission] FOREIGN KEY([QuyenHanID])
REFERENCES [dbo].[QuyenHan] ([QuyenHan_ID])
GO
ALTER TABLE [dbo].[NhomNguoiDung_QuyenHan] CHECK CONSTRAINT [FK_UserGroup_Permission_Permission]
GO
/****** Object:  ForeignKey [FK_UserGroup_Permission_UserGroup]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[NhomNguoiDung_QuyenHan]  WITH CHECK ADD  CONSTRAINT [FK_UserGroup_Permission_UserGroup] FOREIGN KEY([NhomNguoiDung_ID])
REFERENCES [dbo].[NhomNguoiDung] ([NhomNguoiDung_ID])
GO
ALTER TABLE [dbo].[NhomNguoiDung_QuyenHan] CHECK CONSTRAINT [FK_UserGroup_Permission_UserGroup]
GO
/****** Object:  ForeignKey [FK_PhieuDanhGiaNCC_TrangThaiPhieu]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDanhGiaNCC]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDanhGiaNCC_TrangThaiPhieu] FOREIGN KEY([TrangThai])
REFERENCES [dbo].[TrangThaiPhieu] ([TrangThaiPhieu_ID])
GO
ALTER TABLE [dbo].[PhieuDanhGiaNCC] CHECK CONSTRAINT [FK_PhieuDanhGiaNCC_TrangThaiPhieu]
GO
/****** Object:  ForeignKey [FK_SupplierInspectionReceipt_User]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDanhGiaNCC]  WITH CHECK ADD  CONSTRAINT [FK_SupplierInspectionReceipt_User] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuDanhGiaNCC] CHECK CONSTRAINT [FK_SupplierInspectionReceipt_User]
GO
/****** Object:  ForeignKey [FK_SupplierInspectionReceipt_User1]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDanhGiaNCC]  WITH CHECK ADD  CONSTRAINT [FK_SupplierInspectionReceipt_User1] FOREIGN KEY([MaNguoiDuyet])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuDanhGiaNCC] CHECK CONSTRAINT [FK_SupplierInspectionReceipt_User1]
GO
/****** Object:  ForeignKey [FK_PhieuDeNghiMuaHang_User]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDeNghiMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDeNghiMuaHang_User] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuDeNghiMuaHang] CHECK CONSTRAINT [FK_PhieuDeNghiMuaHang_User]
GO
/****** Object:  ForeignKey [FK_PhieuDeNghiMuaHang_User1]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDeNghiMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDeNghiMuaHang_User1] FOREIGN KEY([MaNguoiDuyet])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuDeNghiMuaHang] CHECK CONSTRAINT [FK_PhieuDeNghiMuaHang_User1]
GO
/****** Object:  ForeignKey [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa] FOREIGN KEY([MaHangHoa])
REFERENCES [dbo].[HangHoa] ([MaHangHoa])
GO
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa] CHECK CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa]
GO
/****** Object:  ForeignKey [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa_NhaCungCap]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa_NhaCungCap] FOREIGN KEY([HangHoa_NhaCungCap_ID])
REFERENCES [dbo].[HangHoa_NhaCungCap] ([HangHoa_NhaCungCap_ID])
GO
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa] CHECK CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_HangHoa_NhaCungCap]
GO
/****** Object:  ForeignKey [FK_PhieuDeNghiMuaHang_HangHoa_PhieuDeNghiMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa]  WITH CHECK ADD  CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_PhieuDeNghiMuaHang] FOREIGN KEY([MaPhieuDeNghiMuaHang])
REFERENCES [dbo].[PhieuDeNghiMuaHang] ([MaPhieu])
GO
ALTER TABLE [dbo].[PhieuDeNghiMuaHang_HangHoa] CHECK CONSTRAINT [FK_PhieuDeNghiMuaHang_HangHoa_PhieuDeNghiMuaHang]
GO
/****** Object:  ForeignKey [FK_PhieuMuaHang_NguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_PhieuMuaHang_NguoiDung] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuMuaHang] CHECK CONSTRAINT [FK_PhieuMuaHang_NguoiDung]
GO
/****** Object:  ForeignKey [FK_PhieuMuaHang_NguoiDung1]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_PhieuMuaHang_NguoiDung1] FOREIGN KEY([MaNguoiDuyet])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuMuaHang] CHECK CONSTRAINT [FK_PhieuMuaHang_NguoiDung1]
GO
/****** Object:  ForeignKey [FK_PhieuNhapKho_NguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuNhapKho]  WITH CHECK ADD  CONSTRAINT [FK_PhieuNhapKho_NguoiDung] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuNhapKho] CHECK CONSTRAINT [FK_PhieuNhapKho_NguoiDung]
GO
/****** Object:  ForeignKey [FK_PhieuTamUng_NguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuTamUng]  WITH CHECK ADD  CONSTRAINT [FK_PhieuTamUng_NguoiDung] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuTamUng] CHECK CONSTRAINT [FK_PhieuTamUng_NguoiDung]
GO
/****** Object:  ForeignKey [FK_PhieuTamUng_NguoiDung1]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuTamUng]  WITH CHECK ADD  CONSTRAINT [FK_PhieuTamUng_NguoiDung1] FOREIGN KEY([MaNguoiDuyet])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuTamUng] CHECK CONSTRAINT [FK_PhieuTamUng_NguoiDung1]
GO
/****** Object:  ForeignKey [FK_PhieuThanhToan_NguoiDung]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuThanhToan]  WITH CHECK ADD  CONSTRAINT [FK_PhieuThanhToan_NguoiDung] FOREIGN KEY([MaNguoiTao])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuThanhToan] CHECK CONSTRAINT [FK_PhieuThanhToan_NguoiDung]
GO
/****** Object:  ForeignKey [FK_PhieuThanhToan_NguoiDung1]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[PhieuThanhToan]  WITH CHECK ADD  CONSTRAINT [FK_PhieuThanhToan_NguoiDung1] FOREIGN KEY([MaNguoiDuyet])
REFERENCES [dbo].[NguoiDung] ([MaNguoiDung])
GO
ALTER TABLE [dbo].[PhieuThanhToan] CHECK CONSTRAINT [FK_PhieuThanhToan_NguoiDung1]
GO
/****** Object:  ForeignKey [FK_QuyTrinhMuaHang_PhieuDeNghiMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[QuyTrinhMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_QuyTrinhMuaHang_PhieuDeNghiMuaHang] FOREIGN KEY([MaPhieuDeNghiMuaHang])
REFERENCES [dbo].[PhieuDeNghiMuaHang] ([MaPhieu])
GO
ALTER TABLE [dbo].[QuyTrinhMuaHang] CHECK CONSTRAINT [FK_QuyTrinhMuaHang_PhieuDeNghiMuaHang]
GO
/****** Object:  ForeignKey [FK_QuyTrinhMuaHang_PhieuMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[QuyTrinhMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_QuyTrinhMuaHang_PhieuMuaHang] FOREIGN KEY([MaPhieuMuaHang])
REFERENCES [dbo].[PhieuMuaHang] ([MaPhieu])
GO
ALTER TABLE [dbo].[QuyTrinhMuaHang] CHECK CONSTRAINT [FK_QuyTrinhMuaHang_PhieuMuaHang]
GO
/****** Object:  ForeignKey [FK_QuyTrinhMuaHang_PhieuTamUng]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[QuyTrinhMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_QuyTrinhMuaHang_PhieuTamUng] FOREIGN KEY([MaPhieuTamUng])
REFERENCES [dbo].[PhieuTamUng] ([MaPhieu])
GO
ALTER TABLE [dbo].[QuyTrinhMuaHang] CHECK CONSTRAINT [FK_QuyTrinhMuaHang_PhieuTamUng]
GO
/****** Object:  ForeignKey [FK_QuyTrinhMuaHang_PhieuThanhToan]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[QuyTrinhMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_QuyTrinhMuaHang_PhieuThanhToan] FOREIGN KEY([MaPhieuThanhToan])
REFERENCES [dbo].[PhieuThanhToan] ([MaPhieu])
GO
ALTER TABLE [dbo].[QuyTrinhMuaHang] CHECK CONSTRAINT [FK_QuyTrinhMuaHang_PhieuThanhToan]
GO
/****** Object:  ForeignKey [FK_QuyTrinhMuaHang_TrangThaiQuyTrinhMuaHang]    Script Date: 04/17/2010 23:28:38 ******/
ALTER TABLE [dbo].[QuyTrinhMuaHang]  WITH CHECK ADD  CONSTRAINT [FK_QuyTrinhMuaHang_TrangThaiQuyTrinhMuaHang] FOREIGN KEY([TrangThai])
REFERENCES [dbo].[TrangThaiQuyTrinhMuaHang] ([ID])
GO
ALTER TABLE [dbo].[QuyTrinhMuaHang] CHECK CONSTRAINT [FK_QuyTrinhMuaHang_TrangThaiQuyTrinhMuaHang]
GO
